<?php
class default_admin_http extends controller_prototype_class
{
    /**
		экшен по-умолчанию
	*/
    public function PrAction ()
    {
    	$menu = $this->GetUserMenuFromCache();
		$element = current($menu);
		if ($element && isset($element['submenu']) && is_array($element['submenu']) && sizeof($element['submenu']))
		{
			$subElement = current($element['submenu']);
			$ctrlName = $subElement['link']['ctrl'];
			$actionName = $subElement['link']['action']. 'Pr';
			$this->_ctrls->$ctrlName->$actionName();
		}
		else
		{
			$this->_ctrls->users->loginPr();
		}
    } 
    
    private $_groupsMapper;
    
    private function GetGroupsMapper()
    {
		if (!$this->_groupsMapper) 
		{
			$this->_groupsMapper = GroupsMapper::GetInstance($this->User()); 
		}
		return $this->_groupsMapper;     	
    }     

    /**
		загружает основное меню админки
     */
    private function GetMenu()
    {
        return include ($this->GetModule()->GetPath() . 'mainmenu.php');
    }    
    
    private function GetUserMenu()
    {
        $user = $this->User();

        $groupMenu = array();
        $moduleName = $this->GetModule()->GetName();
        $allMenu = $this->GetMenu();
        while (list ($key, $val) = each($allMenu)) 
        {
            $groupSubMenu = array();
            if (is_array($val['submenu']) && sizeof($val['submenu'])) 
            {
                while (list ($skey, $sval) = each($val['submenu'])) 
                {
                    if (!$user->CheckAccess($moduleName, $sval['link']['ctrl'], $sval['link']['action']))
                    {
                    	continue;
                    }
                    $groupSubMenu[$skey] = $sval;
                }
            }           
            
            if (!$user->CheckAccess($moduleName, $val['link']['ctrl'], $val['link']['action'])) 
            {
                if (sizeof($groupSubMenu)) 
                {
                    reset($groupSubMenu);
                    list (, $sv) = each($groupSubMenu);
                    $val['link'] = $sv['link'];
                } 
                else 
                {
                    continue;
                }
            }
            
            $val['submenu'] = $groupSubMenu;
            $groupMenu[$key] = $val;
        }

        return $groupMenu;
    }
    
    protected $_groupMenu = false;
    
    public function GetUserMenuFromCache()
    {
    	if (false !== $this->_groupMenu) return $this->_groupMenu;

    	$user = $this->User();
        $menuFileName = $this->GetGroupsMapper()->GetCacheFileName('admin_menu', $user->GetAllGroups());
        if (is_file($menuFileName) && (filemtime($menuFileName) > $this->GetGroupsMapper()->GetGroupsUpdateTime($user->GetAllGroups())))
        {
        	$this->_groupMenu = include($menuFileName);
        }
		else
		{           	
			$this->_groupMenu = $this->GetUserMenu();
			  	
	        $exportMenu = '<?php return ' . var_export($this->_groupMenu, TRUE) . ';';
	        if (is_file($menuFileName) && ! is_writable($menuFileName)) @chmod($menuFileName, _CHMOD_FILE);
	        if ($this->Libs()->dir->Create(_CACHE_CONFIG_PATH)) file_put_contents($menuFileName, $exportMenu);    	
		}
			    	
        return $this->_groupMenu;
    } 

    private $_reffererChecked = false;
    private $_isRightRefferer = false;
    
    /**
		проверяем, совпадает ли рефферер с URL, для предотвращения атак
	*/
    public function IsRightRefferer()
    {
		if (!$this->_reffererChecked)
		{
			$this->_reffererChecked = true;
			
			$referer = $this->Request()->getHeader('REFERER');
			$host = $this->Request()->getHeader('HOST');
			if (strlen($referer) && strlen($host))
			{
				if (preg_match('#^[^/]+\://' .preg_quote($host._ROOT_URL). '(admin|programs)#i', $referer))					
				{	
					$this->_isRightRefferer = true; 	
				}
			}
			else 
			{
				$this->_isRightRefferer = true;				
			}
		}
		
		return $this->_isRightRefferer;
    }    
}